package com.news.news.web;

import com.github.pagehelper.PageInfo;
import com.news.news.modle.Link;
import com.news.news.modle.News;
import com.news.news.modle.NewsType;
import com.news.news.service.LinkService;
import com.news.news.service.NewsService;
import com.news.news.util.CommonResult;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;

/**
 * Created by 罗铠威 on 2023/8/29 8:19
 * type:
 */
@RestController
@RequestMapping(path = "/admin/LinkCtl")
public class LinkController {

    @Resource
    LinkService linkService;


    /**
     * 获取链接数据列表
     * @return
     */
    @PostMapping(value = "/list")
    public CommonResult list(@RequestBody Link link) {
        PageInfo<Link> linkTypePageInfo = linkService.LinkList(link);//实现分页功能
        CommonResult commonResult=new CommonResult(200,linkTypePageInfo,"成功");
        return commonResult;
    }


    /**
     * 保存新增链接信息
     * @return
     */
    @RequestMapping(value = "/saveLink")
    public CommonResult saveNews(@RequestBody Link link, @RequestHeader("adminToken") String token) {
        if (link.getId()!= null) {//【修改】
            linkService.updateLink(link,token);
        } else {//【新增】
            linkService.saveLink(link,token);
        }
        CommonResult commonResult = new CommonResult(200, null, "成功");

        return commonResult;
    }

    /**
     * 修改友情链接状态
     * @param link
     * @return
     */
    @RequestMapping(value = "/UpdateLinkState")
    public CommonResult UpdateNewsTypeState(@RequestBody Link link) {

        linkService.UpdateLinkState(link);
        CommonResult commonResult = new CommonResult(200, null, "修改状态成功");

        return commonResult;
    }

    /**
     * 【编辑前显示数据】
     * @param id 链接的ID，通过它来获取信息返回前端
     * @return
     */
    @RequestMapping(value = "/findLinkById")
    public CommonResult findLinkById(Integer id) {

        Link link=linkService.findLinkById(id);
        CommonResult commonResult = new CommonResult(200, link, "修改状态成功");

        return commonResult;
    }


    /**
     * 【删除数据】
     * @param id 链接的ID，通过它来删除数据
     * @return
     */
    @RequestMapping(value = "/deleteLinkById")
    public CommonResult deleteNewsTypeById(Integer id) {

        linkService.deleteLinkById(id);
        CommonResult commonResult = new CommonResult(200, null, "修改状态成功");

        return commonResult;
    }

    /**
     * 文件解析器
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping(value = "/uploadLinkimg")
    public CommonResult uploadLinkimg(@RequestParam("file") CommonsMultipartFile file) throws IOException {
        // file.transferTo(new File("E:/a.png"));
         /*
            图片存储到哪儿?
              1.存储到服务器端的硬盘
              2.必须在客户端还可以访问到
          */
        String linkfilename = new Date().getTime()+file.getOriginalFilename();//新生产一个文件名
        String path = "D:\\Program Files\\apache-tomcat-9.0.43\\webapps\\ROOT\\LinkImg\\"+linkfilename;
        file.transferTo(new File(path));

        CommonResult commonResult = new CommonResult(200,"LinkImg\\"+linkfilename,"上传成功");
        return commonResult;
    }

}
